#coding=utf-8

import datetime
import logging

from django.db import models
from django.db.models import permalink
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.conf import settings

logger = logging.getLogger('{{ app_name }}.models')


class DummyManager(models.Manager):

    def get_query_set(self):
        return super(DummyManager, self).get_query_set().filter(valid=True)


class Dummy(models.Model):
    '''
    A dmummy model for you to copy and paste
    '''
    name = models.CharField(max_length=50, unique=True)
    slug = models.CharField(max_length=50, unique=True)
    excertpt = models.CharField(max_length=255, blank=True, null=True)
    content = models.TextField(blank=True, null=True)

    owners = models.ManyToManyField(User)

    valid = models.BooleanField()
    create_by = models.ForeignKey(User)
    create_at = models.DateField(auto_now_add = True)
    update_at = models.DateField(auto_now = True)

    objects = DummyManager()

    class Meta:
        ordering = ["created"]
        verbose_name = "Dummy"
        verbose_name_plural = "Dummies"

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('view_name_in_urlconf', kwargs={'slug': self.slug})

    def raw_sql(self):
        '''
        A demo about using raw sql to work in the model
        '''
        from django.db import connection
        cursor = connection.cursor()
        cursor.execute("SELECT %s FROM %s WHERE name = %%s" % (self._meta.db_table), ['name'])
        row = cursor.fetchone()
        return row
